package com.nf.xiaomi.web.controller;


import com.nf.mvc.ViewResult;
import com.nf.mvc.mapping.RequestMapping;
import com.nf.mvc.support.ResponseVO;
import com.nf.xiaomi.entity.AddressEntity;
import com.nf.xiaomi.entity.UsersEntity;
import com.nf.xiaomi.service.AddressService;
import com.nf.xiaomi.service.UserService;
import com.nf.xiaomi.service.impl.AddressServiceImpl;
import com.nf.xiaomi.service.impl.UserServiceImpl;
import com.nf.xiaomi.util.JsonUtils;

import java.util.List;

import static com.nf.mvc.handler.HandlerHelper.json;

@RequestMapping("/user")
public class UserController {
    UserService userService = new UserServiceImpl();
    AddressService addressService = new AddressServiceImpl();

    @RequestMapping("/insert")
    public ViewResult insert(UsersEntity users) {
        Integer result = userService.insert(users);
        AddressEntity addressEntity = new AddressEntity();
        Integer userID = userService.getAllID(users.getUserAccount());
        addressEntity.setUserId(userID);
        addressEntity.setUserName(users.getUserName());
        addressEntity.setUserPhone(users.getUserPhone());
        addressEntity.setAddressContent(users.getUserAddress());
        addressEntity.setState(1);
        addressService.insert(addressEntity);
        return JsonUtils.Result(result);
    }

    @RequestMapping("/update")
    public ViewResult update(UsersEntity users) {
        Integer result = userService.update(users);
        return JsonUtils.Result(result);
    }

    @RequestMapping("/deleteByID")
    public ViewResult deleteByID(Long account) {
        Integer result = userService.deleteByID(account);
        return JsonUtils.Result(result);
    }

    @RequestMapping("/login")
    public ViewResult login(Long account , String password) {
        Long result = userService.login(account,password);
        return json(new ResponseVO(200, "ok", result));
    }

    @RequestMapping("/register")
    public ViewResult register(Long phone) {
        Long result = userService.register(phone);
        return json(new ResponseVO(200, "ok", result));
    }

    @RequestMapping("/getAll")
    public ViewResult getAll() {
        List<UsersEntity> userAll = userService.getAll();
        return json(new ResponseVO(200, "ok", userAll));
    }

    @RequestMapping("/getAllAccount")
    public ViewResult getAllAccount(Long account) {
        List<UsersEntity> userAll = userService.getAll(account);
        return json(new ResponseVO(200, "ok", userAll));
    }

    @RequestMapping("/getAllName")
    public ViewResult getAll(String name) {
        List<UsersEntity> userAll = userService.getAll(name);
        return json(new ResponseVO(200, "ok", userAll));
    }

    @RequestMapping("/sendSms")
    public ViewResult sendSms(Long phone) {
        String sendSms = userService.sendSms(phone);
        return json(new ResponseVO(200, "ok", sendSms));
    }
}
